﻿using OpenAuth.Repository.Core;//不引入没有ExecuteSqlCommand方法
using OpenAuth.Repository.Interface;
using System;
using System.Web;
using System.Linq;
using System.Linq.Expressions;
using Infrastructure;
using Z.EntityFramework.Plus;//https://www.cnblogs.com/yaopengfei/archive/2018/06/26/9226328.html ef性能增强组件
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using OpenAuth.Repository.Domain;
using Microsoft.Extensions.Options;
using System.Collections.Specialized;

namespace OpenAuth.Repository
{
    public class ApiRepository<T> : IApiRepository<T> where T : Entity
    {
        private ApiDBContext _context;
        private IOptions<BankParam> bankParam;
        public ApiRepository(ApiDBContext context, IOptions<BankParam> _bankParam)
        {
            _context = context;
            bankParam = _bankParam;
        }
        /// <summary>
        /// 执行存储过程并返回集合(查询)
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="sql">调用存储过程</param>
        /// <param name="param">参数列表</param>
        /// <returns></returns>
        public IEnumerable<T> ExecuteTran<T>(string sql, List<SqlParameter> param) where T : class, new()
        {
            return _context.Database.SqlQuery<T>(sql, param.ToArray());
        }
       
        /// <summary>
        /// 获取建行支付二维码内容
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="name">患者姓名</param>
        /// <param name="amount">充值金额</param>
        /// <returns></returns>

        //public PaySecond GetQRText(string patid, string name,string phone,string card, float amount)
        //{
        //    //订单编号(必须为数字)
        //    string orderId = UniqueData.Gener("");
        //    //准备需要md5的字符串（不能出现汉字）
        //    string _mac = "MERCHANTID=" + bankParam.Value.MERCHANTID + "&POSID=" + bankParam.Value.POSID + "&BRANCHID=" + bankParam.Value.BRANCHID +
        //                "&ORDERID=" + orderId + "&PAYMENT=" + amount.ToString() + "&CURCODE=01&TXCODE=530550&REMARK1=" + $"{orderId}" +
        //                "&REMARK2=" +$"{patid}|{phone ?? ""}|{card ?? ""}"+ "&RETURNTYPE=" + bankParam.Value.RETURNTYPE + "&TIMEOUT=" + "&PUB=" + bankParam.Value.PUB;
        //    //md5加密
        //    string mac = Md5.Get32MD5(_mac).ToLower();
        //    Dictionary<string, string> dic = new Dictionary<string, string>();
        //    dic.Add("CCB_IBSVersion", bankParam.Value.CCB_IBSVersion);
        //    dic.Add("MERCHANTID", bankParam.Value.MERCHANTID);
        //    dic.Add("POSID", bankParam.Value.POSID);
        //    dic.Add("BRANCHID", bankParam.Value.BRANCHID);
        //    dic.Add("ORDERID", orderId);
        //    dic.Add("PAYMENT", amount.ToString());
        //    dic.Add("CURCODE", bankParam.Value.CURCODE);
        //    dic.Add("REMARK1", $"{orderId}");//写入订单号，建行回调时，根据订单号充值
        //    dic.Add("REMARK2", $"{patid}|{phone ?? ""}|{card ?? ""}");
        //    dic.Add("TXCODE", bankParam.Value.TXCODE);
        //    dic.Add("RETURNTYPE", bankParam.Value.RETURNTYPE);
        //    dic.Add("TIMEOUT", "");//DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")
        //    dic.Add("MAC", mac);
        //    HttpHelper webClient = new HttpHelper("http://192.168.0.33:8080/");
        //    string firstResult = webClient.Get(dic, bankParam.Value.URL);
        //    //string PAYURL = HttpUtility.UrlDecode(Json.ToObject<PayFirst>(firstResult).PAYURL);
        //    string PAYURL = Json.ToObject<PayFirst>(firstResult).PAYURL;
        //    string secondResult = webClient.Get(null, PAYURL);
        //    string QRURL = HttpUtility.UrlDecode(Json.ToObject<PaySecond>(secondResult).QRURL);

        //    return new PaySecond { QRURL = QRURL,ORDERID=orderId};
        //}

        /// <summary>
        /// 执行存储过程并返回集合(写入)
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public DataTable PutTran(string sql, List<SqlParameter> param)
        {
            //插入语句的存储过程返回datatable
            var dt = _context.Database.SqlQuery(sql, param.ToArray());
            return dt;

        }
       



    }

}
